package com.wswy.wzcx.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import com.wzcx.util.DimensionUtil;


/**
 * Created by wangduheng26 on 2018/3/13.
 * wangduheng26@gmail.com
 */

public class IdCardStrokeView extends FrameLayout {
    public IdCardStrokeView(@NonNull Context context) {
        super(context);
        init();
    }

    public IdCardStrokeView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public IdCardStrokeView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public IdCardStrokeView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        setWillNotDraw(false);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        cornerLength = DimensionUtil.dpToPx(7);
        cornerLineWidth = DimensionUtil.dpToPx(1);
    }
    int cornerLength = 100;
    int cornerLineWidth = 6;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setStrokeWidth(DimensionUtil.dpToPx(1));
        drawCorners(canvas);

    }
    private void drawCorners(Canvas canvas) {
        paint.setStrokeWidth(cornerLineWidth);
        // left top
        drawLine(canvas, frameRect.left - cornerLineWidth / 2, frameRect.top, cornerLength, 0);
        drawLine(canvas, frameRect.left, frameRect.top, 0, cornerLength);

        // right top
        drawLine(canvas, frameRect.right + cornerLineWidth / 2, frameRect.top, -cornerLength, 0);
        drawLine(canvas, frameRect.right, frameRect.top, 0, cornerLength);

        // right bottom
        drawLine(canvas, frameRect.right, frameRect.bottom, 0, -cornerLength);
        drawLine(canvas, frameRect.right + cornerLineWidth / 2, frameRect.bottom, -cornerLength, 0);

        // left bottom
        drawLine(canvas, frameRect.left - cornerLineWidth / 2, frameRect.bottom, cornerLength, 0);
        drawLine(canvas, frameRect.left, frameRect.bottom, 0, -cornerLength);
    }

    private void drawLine(Canvas canvas, float x, float y, int dx, int dy) {
        canvas.drawLine(x, y, x + dx, y + dy, paint);
    }

    private RectF frameRect = new RectF();

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        resetFrameRect(w, h);
    }
    private void resetFrameRect(int w, int h) {
//        if (2 == 1) {
//            frameRect.left = (int) (w * 0.05);
//            frameRect.top = (int) (h * 0.25);
//        } else {
//            frameRect.left = (int) (w * 0.2);
//            frameRect.top = (int) (h * 0.2);
//        }
        frameRect.left=0;
        frameRect.top=0;
        frameRect.right = w - frameRect.left;
        frameRect.bottom = h - frameRect.top;
    }
}
